package com.hqyj.ctxp.controller;

import com.hqyj.ctxp.pojo.User;
import com.hqyj.ctxp.service.UserService;
import com.hqyj.ctxp.utils.JwtUtils;
import com.hqyj.ctxp.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //用户注册
    @PostMapping("/userRegister")
    public ResultInfo userRegister(@RequestBody User user){
        return userService.register(user);
    }

    //用户登录
    @PostMapping("/userLogin")
    public ResultInfo userLogin(@RequestBody User user){
        return userService.login(user);
    }

    //查单个用户
    @GetMapping("/userOne/{userName}")
    public ResultInfo userOne(@PathVariable String userName){
        return userService.selectOne(userName);
    }
    @GetMapping("/userOne")
    public ResultInfo userOne(@RequestBody User user){
        String userToken = JwtUtils.createUserToken(user);
        String localUser = JwtUtils.getAudience(userToken);
        Integer userNo = Integer.valueOf(localUser);
        return userService.selectOne(userNo);
    }
 
    //用户列表
    @PostMapping("/userList")
    public ResultInfo userList(@RequestBody User user){
        return userService.selectUserList(user);
    }

    //角色列表
    @GetMapping("/roleList")
    public ResultInfo roleList(){
        return userService.selectRoles();
    }

    //根据用户编号修改用户角色
    @GetMapping("/changeUserRoleByUserNo")
    public ResultInfo changeUserRoleByUserNo(Integer userFkRole, Integer userNo){
        return userService.changeUserRoleByUserNo(userFkRole,userNo);
    }

    //根据用户编号删除用户信息
    @DeleteMapping("/deleteUserByUserNo/{userNo}")
    public ResultInfo deleteUserByUserNo(@PathVariable Integer userNo){
        return userService.deleteUserByUserNo(userNo);
    }

    //新增用户同时在用户信息表增加该用户
    @PostMapping("/insertUser")
    public ResultInfo insertUser(@RequestBody User user){
        return userService.insertUser(user);
    }

    //验证用户密码
    @PostMapping("/checkUserPwd")
    public ResultInfo checkUserPwd(@RequestBody User user){
        return userService.checkPwd(user);
    }


    //前台用户修改个人密码
    @PostMapping("/updateUserPwd")
    public ResultInfo updateUserPwd(@RequestBody User user){
        return userService.updateUserPwd(user);
    }
}
